package com.example.activity

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.RequiresApi

class SuccessActivity : AppCompatActivity() {
    private val notifyId:Int = 0x11
    private val channelId:String = "amazing"

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sucess)

        //发送登录成功通知
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(NotificationChannel(channelId,"chanel", NotificationManager.IMPORTANCE_HIGH));

        val notification = Notification.Builder(this,channelId)
        //设置通知打开后自动消失
        notification.setAutoCancel(true)
        notification.setSmallIcon(R.drawable.bg_0)
        notification.setContentTitle("你好呀小猪仔!")
        notification.setContentText("点我查看详情!")
        notification.setWhen(System.currentTimeMillis())

        notification.setDefaults(Notification.DEFAULT_SOUND)

        val notificationIntent = Intent(this, NotificationActivity::class.java)

        //使用这个PendingIntent的目的是当点击的时候再初始化那个要显示的activity
        val pi = PendingIntent.getActivity(this, 0x11, notificationIntent, 0)

        notification.setContentIntent(pi)

        //发送通知
        notificationManager.notify(notifyId,notification.build())

        val wellComeInfo = intent.getStringExtra("welcome_info")
        val nickname = intent.getStringExtra("nick_name")

        Toast.makeText(
            applicationContext,
            "$wellComeInfo $nickname 这是从登录页面过来的信息!",
            Toast.LENGTH_LONG
        ).show()


    }

    public fun close(view: View?):Unit{
        finish()
    }


    /**
     * 切换头像
     */
    fun switchHead(view: View) {
        val intent = Intent(this, ChooseHeadThumbActivity::class.java)
        startActivityForResult(intent,0x11)
    }


    /**
     * 根据返回码处理返回的数据
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 0x11 && resultCode == 0x11){
            val imageId = data?.extras?.getInt("imageId")!!
            val imageView = findViewById<ImageView>(R.id.head_thumb)
            imageView.setImageResource(imageId)
        }
    }

    /**
     * 开启包含片段的页面
     */
    fun startFragment(view: View) {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
    }

    /**
     * 点击头像的时候动态设置长按监听事件
     */
    fun setLongClickListener(view: View) {
        view.setOnLongClickListener{
            registerForContextMenu(it)
            openContextMenu(it)
            return@setOnLongClickListener true
        }
    }

    /**
     * 定义会话菜单的提示元素
     */
    override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)
        menu!!.add("收藏")
        menu!!.add("举报")
    }
}